import { parseTime } from '@/utils'
import store from '@/store'

export const dateTimeFilter = time => {
  const format = getFormat('time')
  // let lang = localStorage.getItem(store.state.user.keys.storage.lang) || 'en-US'
  // if(lang === 'zh-CN'){
  //   format = '{y}-{m}-{d} {h}:{i}:{s}'
  // }
  return parseTime(time, format)
}

export const dateTimeShortFilter = time => {
  let format = getFormat('shortTime')
  // let lang = localStorage.getItem(store.state.user.keys.storage.lang) || 'en-US'
  // if(lang === 'zh-CN'){
  //   format = '{y}-{m}-{d} {h}:{i}'
  // }
  return parseTime(time, format)
}
export const timeFilter = time => parseTime(time, '{h}:{i}:{s}')
export const dateFilter = time => {
  let format = getFormat('date')
  // let lang = localStorage.getItem(store.state.user.keys.storage.lang) || 'en-US'
  // if(lang === 'zh-CN'){
  //   format = '{y}-{m}-{d}'
  // }
  return parseTime(time, format)
}

export const numberFilter = num => {
  const str = num.toFixed(0)
  const reg = str.indexOf('.') > -1 ? /(\d)(?=(\d{3})+\.)/g : /(\d)(?=(?:\d{3})+$)/g
  return str.replace(reg, '$1' + ',')
}
export function getFormat(type) {
  const dateFormat = store.getters.userInfo.dateFormat
  let format = ''

  if (dateFormat) {
    format = dateFormat.replace(/([yMd])+/g, (result, key) => {
      return '{' + key.toLowerCase() + '}'
    })
  } else {
    format = '{m}/{d}/{y}'
  }

  if (type === 'time') {
    return format + ' {h}:{i}:{s}'
  } else if (type === 'shortTime') {
    return format + ' {h}:{i}'
  } else {
    return format
  }
}

/**
 * time 格式 yyyyMMddHHmmss
 * @param time
 */
export const terminalDateTimeFilter = (time) => {
  console.log('time:', time)
  if (time && time.length == 14) {
    const userInfo = JSON.parse(
      localStorage.getItem(store.getters.keys.storage.username)
    )
    const year = time.substr(0, 4)
    const month = time.substr(4, 2)
    const day = time.substr(6, 2)
    const hour = time.substr(8, 2)
    const min = time.substr(10, 2)
    const sec = time.substr(12, 2)
    if (userInfo.dateFormat == 'yyyy-MM-dd') {
      return year + '-' + month + '-' + day + ' ' + hour + ':' + min + ':' + sec
    } else if (userInfo.dateFormat == 'yyyy/MM/dd') {
      return year + '/' + month + '/' + day + ' ' + hour + ':' + min + ':' + sec
    } else if (userInfo.dateFormat == 'dd/MM/yyyy') {
      return day + '/' + month + '/' + year + ' ' + hour + ':' + min + ':' + sec
    } else if (userInfo.dateFormat == 'MM/dd/yyyy') {
      return month + '/' + day + '/' + year + ' ' + hour + ':' + min + ':' + sec
    } else {
      return month + '/' + day + '/' + year + ' ' + hour + ':' + min + ':' + sec
    }
  }
  return ''
}